package com.bitdf.txing.common.interceptor;

import com.bitdf.txing.common.to.UserTo;
import com.bitdf.txing.common.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录状态检验拦截器
 * @author w
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    //当前线程空间
    public static ThreadLocal<UserTo> threadLocal = new ThreadLocal<>();
    @Autowired
    StringRedisTemplate redisTemplate;
    public LoginInterceptor(StringRedisTemplate redisTemplate){
        this.redisTemplate=redisTemplate;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean b=UserUtils.isPerm(request,response,null,redisTemplate,threadLocal);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }



}
